package com.example.calendarapp;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.MeasureSpec;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

public class MonthActivity extends Activity {
	protected static String TAG = "MONTHACTIVITY";
	private static String[] months = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
	public static Calendar c = Calendar.getInstance();
	public static EventsDataSource datasource;
	int numberOfColumns = 7;
	int numberOfRows = 5;
	GridView mGrid;
	MonthViewAdapter adapter;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_month);
		EventsDataSource.getInstance(this);
		
		final Button button = (Button) findViewById(R.id.butt);
		button.setOnClickListener(new View.OnClickListener(){
			public void onClick(View v)
			{
				adapter.incrementMonth();
				adapter.notifyDataSetChanged();
				c.add(Calendar.MONTH, 1);
		        TextView t = (TextView) findViewById(R.id.monthtitle);
		        t.setText(months[c.get(Calendar.MONTH)]);
			}
		});
		
		 

		adapter = new MonthViewAdapter(this);
//		GridView g = (GridView) findViewById(R.id.gridview1);
//		g.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
//		int height = g.getHeight();
//		adapter.setHeight(height);
//		g.setAdapter(adapter);
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
        params.addRule(RelativeLayout.BELOW, R.id.monthtitle);
        params.addRule(RelativeLayout.ABOVE, R.id.butt);
        mGrid = new GridView(this) {
            @Override 
            protected void onLayout(boolean changed, int l, int t, int r, int b) {
                super.onLayout(changed, l, t, r, b);
                if(!calculated)
                    getDimens(); 
            } 
        }; 
        mGrid.setVerticalSpacing(0);
        mGrid.setHorizontalSpacing(0);
        mGrid.setStretchMode(GridView.NO_STRETCH);
        mGrid.setBackgroundColor(Color.rgb(100, 10, 10));      
        ((RelativeLayout)findViewById(R.id.rootview)).addView(mGrid, params);
        
        TextView t = (TextView) findViewById(R.id.monthtitle);
        t.setText(months[c.get(Calendar.MONTH)]);
    } 
	
	private int mCellWidth;
    private int mCellHeight;
    boolean calculated = false;
    protected void getDimens() { 
        calculated = true;
        mCellWidth = mGrid.getWidth()/numberOfColumns;
        mCellHeight = mGrid.getHeight()/numberOfRows;
        mGrid.setColumnWidth(mCellWidth+1);
        mGrid.setNumColumns(numberOfColumns);
        adapter.setHeight(mGrid.getHeight());
        mGrid.setAdapter(adapter);
    }

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.month, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
	
	
}
